<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <!-- <input type="file" id="file" onchange="showimg('file', 'img')"> -->
    <!-- 或者 -->
    <input type="file" id="file" onchange="showimg(this.id, 'img')">
    <img src="" alt="" id="img">
</body>
</html>


<script>
/*
* 显示本地图片
* @param string sourceId   input 按钮的id
* @param string targetId   img 按钮的id
*/
function showimg(sourceId = 'file', targetId = 'img') { 
    if (typeof FileReader === 'undefined') {  
        alert('Your browser does not support FileReader...');  
        return;  
    }  
    var reader = new FileReader();  
  
    reader.onload = function(e) {  
        var img = document.getElementById(targetId);
        img.src = reader.result;
    }  
    reader.readAsDataURL(document.getElementById(sourceId).files[0]);
}
</script>
